package znbkTestPaperSc.activity;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lancooznbk.R;
import com.example.lancooznbk.view.base.BaseActivity;
import com.example.lancooznbk.view.base.BaseFragment;
import com.example.lancooznbk.view.base.ILoadPaperView;
import com.example.lancooznbk.view.widget.ClickImageView;
import com.example.lancooznbk.view.widget.LoadingDialog;
import com.example.lancooznbk.view.widget.ScaleButton;

import lancooControl.AndroidBug5497Workaround;
import tcking.github.com.giraffeplayer.GiraffePlayer;
import tv.danmaku.ijk.media.player.IMediaPlayer;
import znbkListSc.entity.CollectionListEntity;
import znbkTestPaperInfor.ZnbkPaperDef;
import znbkTestPaperInfor.ZnbkPaperInfor;
import znbkTestPaperSc.ZnbkPaperScInfor;
import znbkTestPaperSc.entity.CollectionPaperEntity;
import znbkTestPaperSc.presenter.CollectionPaperPresenter;

/**
 * Created by Administrator on 2018\3\22 0022.
 */

public class CollectionPaperSplActivity extends BaseActivity implements ILoadPaperView<CollectionPaperEntity> {

    private ScaleButton mSBtnBack;//返回按钮
    private TextView mTvTittle;
    private ScaleButton mSBtnMore;
    private LinearLayout mLayoutTitleType;
    private TextView mTvTitleType;
    private TextView mTvTypeName;
    private LinearLayout mLayoutBasicInfor;//基本信息：创建时间，推荐人气
    private TextView mTvCreateTime;
    private TextView mTvRecommendNumberOfTimes;
    private LinearLayout mLayoutTools;//工具栏：笔记，知识点
    private ClickImageView mCivNotes;
    private ClickImageView mCivKnowledgePoint;

    GiraffePlayer player;
    private CollectionListEntity.FavouriteBean mEntity;
    public static final  String PAPER_ENTITY = "paperEntity";
    public static final String PAPER_FROM = "paperFrom";
    public int mQuesOrPaperFrom;
    private int ModelFrom;//试卷来源
    CollectionPaperPresenter mPresenter;
    private CollectionPaperEntity mEntitySc;
    /*
   * 获取作文题用户相关信息
   * */
    private String ResName = "";
    private String ResTypeName = "";
    private String CreateTime = "";
    private int RecommendNumberOfTimes = -1;
    public String VideoPath = "";

    public  static void launch(BaseFragment mContext, CollectionListEntity.FavouriteBean entity, @ZnbkPaperDef.PaperFrom int quesOrPaperFrom){
        ZnbkPaperScInfor.CreatTime = entity.getCreateTime();
        ZnbkPaperScInfor.RecommendNumberOfTimes = -1;
        ZnbkPaperScInfor.ResID = entity.getQuesID();
        if(entity.getResType().equals("1")||
                entity.getResType().equals("2")||
                entity.getResType().equals("3")||
                entity.getResType().equals("4")||
                entity.getResType().equals("5")||
                entity.getResType().equals("6")) {
            ZnbkPaperScInfor.Flag = 1;
        }else{
            ZnbkPaperScInfor.Flag = 0;
        }

        if(entity.getResType().equals("Q")) {
            ZnbkPaperInfor.ResFlag = 0;
        }else{
            ZnbkPaperInfor.ResFlag = 1;
        }
        Bundle bundle = new Bundle();
        bundle.putSerializable(PAPER_ENTITY,entity);
        bundle.putInt(PAPER_FROM,quesOrPaperFrom);
        Intent intent = new Intent(mContext.getContext(), CollectionPaperSplActivity.class);
        intent.putExtras(bundle);
        mContext.startActivityForResult(intent,1);
    }

    @Override
    public int attachLayoutRes() {
        return R.layout.activity_paper_spl;
    }

    @Override
    protected void initViews() {

        initView();

        AndroidBug5497Workaround.assistActivity(this);
        mPresenter = new CollectionPaperPresenter(this);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null){
            mEntity = (CollectionListEntity.FavouriteBean) bundle.getSerializable(PAPER_ENTITY);
            mQuesOrPaperFrom = bundle.getInt(PAPER_FROM);
        }

        //核心代码.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }


        player = new GiraffePlayer(this);
        player.onComplete(new Runnable() {
            @Override
            public void run() {
                //callback when video is finish
                Toast.makeText(getApplicationContext(), "video play completed", Toast.LENGTH_SHORT).show();
            }
        }).onInfo(new GiraffePlayer.OnInfoListener() {
            @Override
            public void onInfo(int what, int extra) {
                switch (what) {
                    case IMediaPlayer.MEDIA_INFO_BUFFERING_START:
                        //do something when buffering start
                        break;
                    case IMediaPlayer.MEDIA_INFO_BUFFERING_END:
                        //do something when buffering end
                        break;
                }
            }
        }).onError(new GiraffePlayer.OnErrorListener() {
            @Override
            public void onError(int what, int extra) {
                Toast.makeText(getApplicationContext(), "video play error",Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    protected void updateViews(boolean isRefresh) {
        mSBtnMore.setVisibility(View.GONE);
        ZnbkPaperScInfor.IsFromSpl = true;
        mPresenter.getData(isRefresh);
    }

    /*
 * 初始化界面控件
 * */
    private void initView(){

        /*
        * 返回按钮菜单相关设置
        * */
        mSBtnBack = (ScaleButton) findViewById(R.id.sbtn_back);
        mSBtnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        /*
        * 页面头部-》收藏详情：mTvTittle
        * */
        mTvTittle = (TextView) findViewById(R.id.tv_title);
        mTvTittle.setText(R.string.znbk_titleCollectionDetails);

        /*
        * 更多功能按钮菜单相关信息
        * */
        mSBtnMore = (ScaleButton) findViewById(R.id.sbtn_more);

         /*
        * 显示笔记原题栏：mLayoutTitleType
        * 显示笔记原题栏文字： mTvTitleType
        * */
        mLayoutTitleType = (LinearLayout) findViewById(R.id.layout_titleType);
        mLayoutTitleType.setVisibility(View.VISIBLE);
        mTvTitleType  = (TextView) findViewById(R.id.tv_titleType);
        mTvTitleType.setText(R.string.znbk_quesNote);


        mTvTypeName = (TextView) findViewById(R.id.tv_typeName);
        mLayoutBasicInfor = (LinearLayout) findViewById(R.id.layout_basicInfor);
        /*
        * 隐藏基本信息
        * */
        mLayoutBasicInfor.setVisibility(View.GONE);
        mTvCreateTime = (TextView) findViewById(R.id.tv_createTime);
        mTvRecommendNumberOfTimes = (TextView) findViewById(R.id.tv_recommendNumberOfTimer);
        mLayoutTools = (LinearLayout) findViewById(R.id.layout_tools);
        /*
        * 隐藏工具栏
        * */
        mLayoutTools.setVisibility(View.GONE);
        mCivNotes = (ClickImageView) findViewById(R.id.civ_notes);
        mCivKnowledgePoint = (ClickImageView) findViewById(R.id.civ_knowledge_point);

    }

    @Override
    protected void onPause() {
        super.onPause();
        if (player != null) {
            player.onPause();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (player != null) {
            player.onResume();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (player != null) {
            player.onDestroy();
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (player != null) {
            player.onConfigurationChanged(newConfig);
        }
    }

    @Override
    public void onBackPressed() {
        if (player != null && player.onBackPressed()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    public void loadPaperData(CollectionPaperEntity data) {
        mEntitySc = data;

        /*
        * 显示资源概况
        * */
        ResName = mEntitySc.getResDetail().getResName();
        if (ResName.equals("")) {
            ResName = mEntitySc.getResDetail().getResTypeName();
        }

        /*
        * 资源类型
        * */
        ResTypeName = mEntitySc.getResDetail().getResTypeName();


        /*
        * 视频路径
        * */
        VideoPath = mEntitySc.getResDetail().getVedioList().get(0).getAudioPath();
        player.play(VideoPath);
        player.setShowNavIcon(false);

        /*
        * 显示题型名称
        * */
        mTvTypeName.setText(ResTypeName);

    }

    @Override
    public void loadComplete(boolean hasComplete) {

    }

    @Override
    public void openExamReportDialog() {

    }

    @Override
    public void openAssessDialog() {

    }

    @Override
    public void updatingResult(boolean isSucceed) {

    }

    @Override
    public void updatingReportResult(boolean isReportSucceed) {

    }

    @Override
    public LoadingDialog getLoadingDialog() {
        return null;
    }
}